<?
class Image 
{
	  private $projectName;
	  private $projectArea;
	  private $imageFormat;
	  private $photoID;
	  private $reWriteMode;
	  private $fileName;
	  private $rotate=0;
	  private $normalize=0;
	  private $unsharpmask=1;
	  private $brightness=100;
	  private $saturation=100;
	  private $hue=100;
	  private $crop=array();
	  private $convertParams=array();
	  
	  private $valid=true;
	  public  $errorMessage;
	  public  $valid_types =  array("gif","jpg", "png", "jpeg", "bmp","tif");
	  public  $maxFileSize = 1000000; //byte
	  
	  function setImage($projectName,$projectArea,$photoID,$atribute=array())
	  {
	  	
	  	foreach ($atribute as $key=>$item)
	  	{
	  		$$key=$item;
	  	}
	  	
	  	if (is_file($fileName) || isset($fileContent))
	  	{
	  		if ($fileContent)
	  		{
	  			$this->fileContent=$fileContent;
	  		}else{
	  			$this->fileName=$fileName;
	  		}
	  			
	  	}else{
	  		if  (isset($_FILES["photo"]))
			{
				if (is_uploaded_file($_FILES['photo']['tmp_name']))
				{					
					$ext = substr($_FILES['photo']['name'], 1 + strrpos($_FILES['photo']['name'], "."));
					$ext = strtolower($ext);
					if (in_array($ext, $this->valid_types))
					{
						$filesize=filesize($_FILES['photo']['tmp_name']);
						if ($filesize > 0 && $filesize < $this->maxFileSize)
						{
							$this->fileName=$_FILES['photo']['tmp_name'];
						}else{
							$this->errorMessage='file incorect size';
							func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
							$this->valid=false;
							return false;
						}
					}else{
						$this->errorMessage='file incorect format';
						func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
						$this->valid=false;
						return false;
					}
				}else{
					$this->errorMessage='file not upload';
					func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
					$this->valid=false;
					return false;
				}
			}else{
				$this->errorMessage='file not found';
				func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
				$this->valid=false;
				return false;
			}
	  		
	  	}
	  	

	  	if ($projectName!='')
	  	{
	  		$this->projectName=$projectName;
	  	}else{
	  		$this->errorMessage='projectName not found';
	  		func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
			$this->valid=false;
			return false;
	  	}
	  	
	  	if ($projectArea!='')
	  	{
	  		$this->projectArea=$projectArea;
	  	}else{
	  		$this->errorMessage='projectArea not found';
	  		func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
			$this->valid=false;
			return false;
	  	}
	  	if (isset($imageFormat))
	  	{
	  		$this->imageFormat=$imageFormat;
	  	}
	  	
	  	$photoID*=1;
	  	if ($photoID)
	  	{
	  		$this->photoID=$photoID;
	  	}else{
	  		$this->errorMessage='photoID not found';
	  		func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
			$this->valid=false;
			return false;
	  	}
	  	$this->reWriteMode=$reWriteMode*1;
	  	return true;
	  }
	  
	  
	  function setEditImage($projectName,$projectArea,$photoID,$atribute=array())
	  {
	  	func_log(1,1,LOG_ERR);
	  	foreach ($atribute as $key=>$item)
	  	{
	  		$$key=$item;
	  		
	  		func_log($$key, img_param, LOG_ERR);
	  		func_log($item, img_param, LOG_ERR);
	  	}
	  	
	  	
	  	if ($projectName!='')
	  	{
	  		$this->projectName=$projectName;
	  	}else{
	  		$this->errorMessage='projectName not found';
	  		func_log('������ Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
			$this->valid=false;
			return false;
	  	}
	  		  	if ($rotate!='')
	  	{
	  		$this->rotate=$rotate;
	  	}
	  	if ($unsharpmask!='')
	  	{
	  		$this->unsharpmask=$unsharpmask;
	  	}
	  	if ($brightness!='')
	  	{
	  		$this->brightness=$brightness;
	  	}
	  	if ($normalize!='')
	  	{
	  		$this->normalize=$normalize;
	  	}
	  	if ($saturation!='')
	  	{
	  		$this->saturation=$saturation;
	  	}
	  	if (count($crop))
	  	{
	  		$this->crop=$crop;
	  	}
	  	if ($projectArea!='')
	  	{
	  		$this->projectArea=$projectArea;
	  	}else{
	  		$this->errorMessage='projectArea not found';
	  		func_log('������ Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
			$this->valid=false;
			return false;
	  	}
	  	if (isset($imageFormat))
	  	{
	  		$this->imageFormat=$imageFormat;
	  	}
	  	
	  	$photoID*=1;
	  	if ($photoID)
	  	{
	  		$this->photoID=$photoID;
	  	}else{
	  		$this->errorMessage='photoID not found';
	  		func_log('������ Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
			$this->valid=false;
			return false;
	  	}
	  	return true;
	  }
	  
	  function addConvertParam($maxWeigh,$maxHeight,$addLetter,$method,$atribute=array(),$background='')
	  {
	  	
	  	foreach ($atribute as $key=>$item)
	  	{
	  		$$key=$item;
	  	}
	  	
	  	$maxWeigh*=1;
	  	$maxHeight*=1;
	  	$method*=1;
	  	if (isset($textParams))
	  	{
		  	if (count($textParams))
		  	{
		  		foreach ($textParams as $item)
		  		{
		  			if ($item['x']=='' || $item['y']=='' || $item['text']=='')
		  			{
		  				$this->errorMessage='in textParams not set x or y or text';
		  				func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
		  				$this->valid=false;
		  				return false;
		  			}
		  		}
		  	}
	  	}else{
	  		$textParams=array();
//	  		$background='';
	  	}
	  	array_push($this->convertParams,array('maxWeight'=>$maxWeigh,'maxHeight'=>$maxHeight,'addLetter'=>$addLetter,'method'=>$method,'background'=>$background,'textParams'=>$textParams));
	  	return true;
	  }
	  
	  function send()
	  {
	  	//ini_set("soap.wsdl_cache_enabled", "0"); 
		//ini_set('soap.wsdl_cache_ttl', 0);
	  	if (!$this->valid)
	  	{
	  		return false;
	  	}
	  	if (!count($this->convertParams))
	  	{
	  		$this->errorMessage='convertParams not';
	  		func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
			$this->valid=false;
			return false;
	  	}
	  	if ($this->fileName!='')
	  	{
		  	$fileContent='';
			$fp=fopen($this->fileName,'rb');
			if (!$fp)
			{
				$this->errorMessage='cannot read file '.$this->fileName;
				func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
				$this->valid=false;
				return false;
			}else{
				$fileContent=fread($fp,filesize($this->fileName));
				fclose($fp);
			}
	  	}else{
	  		$fileContent=$this->fileContent;
	  	}
		if ($fileContent)	
		{
			//$client = new SoapClient("http://us.ria.ua/~riaimg/soap/riaImages.wsdl",array("trace"=> 0));  
			$client = new SoapClient("http://img.ria.ua/soap/riaImages.wsdl",array("trace"=> 0));  
			//$client = new SoapClient("http://10.1.18.87/new/soap/image/riaImages.wsdl",array("trace"=> 0));  
			
			try {  
					$PhotoPath = $client->riaAddPhoto(
						array(
							'projectName'  	=> $this->projectName,
							'projectArea'  	=> $this->projectArea,
							'imageFormat'  	=> $this->imageFormat,
						    'photoID'      	=> $this->photoID,
					    	'convertParams'	=> $this->convertParams,
				    	    'reWriteMode' 	=> $this->reWriteMode,
			    			'image'       	=> $fileContent
						) 
					);
		
				return $PhotoPath;
			} catch (SoapFault $fault) {
				$this->errorMessage="faultcode: $fault->faultcode\n faultstring: $fault->faultstring\n";
				func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
				return false;
			}  
		}else{
			$this->errorMessage='cannot read file '.$this->fileName;
			func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
			$this->valid=false;
			return false;
		}
	  }
	  
	  
	  
	  function edit()
	  {
	  	if (!$this->valid)
	  	{
	  		return false;
	  	}
	  	if (!count($this->convertParams))
	  	{
	  		$this->errorMessage='convertParams not';
	  		func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
			$this->valid=false;
			return false;
	  	}
			//$client = new SoapClient("http://us.ria.ua/~riaimg/soap/riaImages.wsdl",array("trace"=> 0));  
			$client = new SoapClient("http://img.ria.ua/soap/riaImages.wsdl",array("trace"=> 0));
			//$client = new SoapClient("http://10.1.18.87/new/soap/image/riaImages.wsdl",array("trace"=> 0));  
			try {  
					$PhotoPath = $client->riaEditPhoto(
						array(
							'projectName'  	=> $this->projectName,
							'projectArea'  	=> $this->projectArea,
							'imageFormat'  	=> $this->imageFormat,
						    'photoID'      	=> $this->photoID,
					    	'convertParams'	=> $this->convertParams,
				    	    'rotate' 		=> $this->rotate,
				    	    'normalize' 	=> $this->normalize,
			    			'unsharpmask'   => $this->unsharpmask,
			    			'brightness'  	=> $this->brightness,
			    			'saturation'   	=> $this->saturation,
			    			'hue'   		=> $this->hue,
			    			'crop'   		=> $this->crop,
						) 
					);

				return $PhotoPath;
			} catch (SoapFault $fault) {
				$this->errorMessage="faultcode: $fault->faultcode\n faultstring: $fault->faultstring\n";
				func_log('Error Image: partner_id='.$_SESSION['partner_id'].'; Error: '.$this->errorMessage, 'soap_image', LOG_ERR);
				return false;
			}  
		
	  }
}
?>